package com.msos.service.customerService;

import com.msos.dao.customerDao.CustomerDao;
import com.msos.model.customer.Customer;
import com.msos.model.manager.Manager;
import com.msos.utils.CommonsUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;

/**
 * 弟中弟：小邦
 */
public class CustomerService {

    CustomerDao customerDao = new CustomerDao();

    //进行界面登录操作
    //首先校验验证码是否正确，再进行手机号和密码的校验
    public Integer login(String phone, String password, HttpServletRequest req){

        try {
            //获取登录请求传来的验证码
            String vcode = req.getParameter("vcode");
            //用 session 保存验证码
            HttpSession session = req.getSession();
            //获取 session 中保存的验证码
            String sessionVcode = (String) session.getAttribute("vcode");
            //用 equalsIgnoreCase 取消验证码的大小写
            if(vcode == null || !vcode.equalsIgnoreCase(sessionVcode)){
                //如果验证码为空或者验证码不正确，那么取消登录
                return CommonsUtils.BAD_VCODE;
            }

            //判断是用户还是管理员
            String role = req.getParameter("role");
            Object object = customerDao.getCustomerByPhone(phone,role);
            if (Integer.parseInt(role) == 1){
                Customer customer = (Customer) object;
                if(object == null){
                    //表示用户不存在
                    return CommonsUtils.USER_NOTFOUND_OR_PASSWORD_BAD;
                }else {
                    //用户信息不为null，表示用户存在
                    if(!customer.getEnabled()){
                        //代表账户被封禁，跳过检查密码直接return返回
                        return CommonsUtils.USER_BLOCKED;
                    }else {
                        if(password.equals(customer.getPassword())){
                            //密码正确，允许登录,并且将用户信息存入session
                            session.setAttribute("loginCustomer",customer);
                            return CommonsUtils.LOGIN_SUCCESS;
                        }else {
                            //密码错误，return返回
                            return CommonsUtils.USER_NOTFOUND_OR_PASSWORD_BAD;
                        }
                    }
                }
            }else {
                Manager manager = (Manager) object;
                if(object == null){
                    //表示用户不存在
                    return CommonsUtils.USER_NOTFOUND_OR_PASSWORD_BAD;
                }else {
                    //用户信息不为null，表示用户存在
                    if(!manager.getEnabled()){
                        //代表账户被封禁，跳过检查密码直接return返回
                        return CommonsUtils.USER_BLOCKED;
                    }else {
                        if(password.equals(manager.getPassword())){
                            //密码正确，允许登录,并且将用户信息存入session
                            session.setAttribute("loginCustomer",manager);
                            return CommonsUtils.LOGIN_SUCCESS;
                        }else {
                            //密码错误，return返回
                            return CommonsUtils.USER_NOTFOUND_OR_PASSWORD_BAD;
                        }
                    }
                }
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //其他异常错误
        return CommonsUtils.EXCEPTION_ERROR;
    }

    //修改密码
    public Integer updateCustomerByPassword(String phone, String password ,String newPassword1 ,String newPassword2,HttpServletRequest req){
        try {
            Object object = customerDao.getCustomerByPhone(phone,"1");
            //代表账户存在而且没被禁用，开始检查密码
                Customer customer = (Customer)object;
                if (customer == null){
                    return CommonsUtils.USER_NOTFOUND_OR_PASSWORD_BAD;
                }else {
                    if (!customer.getEnabled()){
                        return CommonsUtils.USER_BLOCKED;
                    }else {
                        if(password.equals(customer.getPassword())){
                            if(newPassword1.equals(newPassword2)){
                                customerDao.updatePasswordByCustomer(newPassword2,phone);
                                return CommonsUtils.UPDATE_PASSWORD_SUCCESS;
                            }else {
                                return CommonsUtils.UPDATE_PASSWORD_ERROR;
                            }
                        }else {
                            //密码错误，return返回
                            return CommonsUtils.USER_NOTFOUND_OR_PASSWORD_BAD;
                        }
                    }
                }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return CommonsUtils.EXCEPTION_ERROR;
    }
}
